package com.vehicle.service.impl;

import com.vehicle.service.FaceRegisterService;
import com.vehicle.service.FaceRecognitionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * 人脸注册服务实现类
 * 实现人脸注册相关的业务逻辑
 */
@Service
public class FaceRegisterServiceImpl implements FaceRegisterService {

    private static final Logger logger = LoggerFactory.getLogger(FaceRegisterServiceImpl.class);

    @Autowired
    private FaceRecognitionService faceRecognitionService;

    /**
     * 人脸检测
     * @param file 人脸照片文件
     * @return 人脸token
     */
    @Override
    public String detectFace(MultipartFile file) {
        try {
            logger.info("开始人脸检测");
            String faceToken = faceRecognitionService.faceDetect(file);
            if (faceToken != null) {
                logger.info("人脸检测成功，faceToken: {}", faceToken);
            } else {
                logger.warn("人脸检测失败，无法识别有效的人脸");
            }
            return faceToken;
        } catch (Exception e) {
            logger.error("人脸检测异常", e);
            return null;
        }
    }

    /**
     * 注册人脸到百度云人脸库
     * @param file 人脸照片文件
     * @param userId 用户ID
     * @param userName 用户名
     * @return 注册结果
     */
    @Override
    public boolean registerFaceToBaiDu(MultipartFile file, String userId, String userName) {
        try {
            logger.info("开始注册人脸到百度云人脸库，用户ID: {}, 用户名: {}", userId, userName);
            boolean result = faceRecognitionService.registerFaceToBaiDu(file, userId, userName);
            if (result) {
                logger.info("人脸注册到百度云成功");
            } else {
                logger.warn("人脸注册到百度云失败");
            }
            return result;
        } catch (Exception e) {
            logger.error("人脸注册到百度云异常", e);
            return false;
        }
    }

    /**
     * 获取百度云API授权token
     * @return token
     */
    @Override
    public String getAuthToken() {
        try {
            logger.info("开始获取百度云API授权token");
            String token = faceRecognitionService.getAuthToken();
            logger.info("获取百度云API授权token成功");
            return token;
        } catch (Exception e) {
            logger.error("获取百度云API授权token异常", e);
            throw new RuntimeException("获取百度云API授权token失败", e);
        }
    }
}